package com.itheima.boottlias.service.impl;

import com.itheima.boottlias.domain.pojo.Dept;
import com.itheima.boottlias.mapper.DeptMapper;
import com.itheima.boottlias.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 部门业务实现
 * @author ghy
 * @version 1.0
 * @since 2025-07-10 10:31
 */
@Service
public class DeptServiceImpl implements DeptService {

    @Autowired
    private DeptMapper deptMapper;

    /**
     * 查询所有
     *
     * @return 部门列表
     */
    @Override
    public List<Dept> findAll() {
        //调用持久层
        // int i = 1/0;
        return deptMapper.findAll();
    }

    /**
     * 根据ID删除
     *
     * @param id 部门ID
     */
    @Override
    public void deleteById(Integer id) {
        deptMapper.deleteById(id);
    }

    /**
     * 新增
     *
     * @param dept 部门实体
     */
    @Override
    public void add(Dept dept) {
        // 补全实体
        LocalDateTime now = LocalDateTime.now();
        dept.setCreateTime(now);
        dept.setUpdateTime(now);
        // 调用持久层
        deptMapper.insert(dept);
    }

    /**
     * 根据ID查询
     *
     * @param id 部门ID
     * @return 部门实体
     */
    @Override
    public Dept findById(Integer id) {
        return deptMapper.findById(id);
    }

    /**
     * 修改部门
     *
     * @param dept 部门实体
     */
    @Override
    public void updateById(Dept dept) {
        // 补全实体属性
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.updateById(dept);
    }
}
